local MathMgr = class("MathMgr");
local PI = math.pi;
local rad = math.rad;--角度转弧度 rad(180)---->3.14
local deg = math.deg;--弧度转角度 deg(3.14) ----->180
function MathMgr:ctor()
    
end

function MathMgr:getVectorXY(startPoint,endPoint)
    local dtX = endPoint.x - startPoint.x;
    local dtY = endPoint.y - startPoint.y;
    local angle = math.atan(dtY / dtX);
    local vectorX = 0;
    local vectorY = 0;
    if dtX > 0 then
        vectorX = math.cos(angle);
        vectorY = math.sin(angle);
    else
        vectorX = - math.cos(angle);
        vectorY = - math.sin(angle);
    end
    return vectorX,vectorY;
end

function MathMgr:getCannonAngle(startPoint,endPoint)
    local vectorX,vectorY = self:getVectorXY(startPoint,endPoint);
    return self:vectorToAngleVertical(vectorX,vectorY);
end

--初始为竖直向上的
function MathMgr:vectorToAngleVertical(vecX,vecY)
    local angle = 0;
    if self:isZero(vecX) then vecX = 0;end
    if vecX > 0 then
        angle = 90 - deg(math.atan(vecY/vecX));
    elseif vecX < 0 then
        angle = -90 - deg(math.atan(vecY/vecX));
    else
        angle = vecY > 0 and 90 or 0;
    end
    return angle;
end

--初始方向为水平的
function MathMgr:vectorToAngleHorizontal(vecX,vecY)
    local angle = 0;
    if self:isZero(vecX) then vecX = 0;end
    if vecX > 0 then
        angle = 0 - deg(math.atan(vecY/vecX));
    elseif vecX < 0 then
        angle = 180 - deg(math.atan(vecY/vecX));
    else
        angle = vecY > 0 and 90 or -90;
    end
    return angle;
end

--算tan arctan
function MathMgr:isZero(num)
    local dt = 1e-5;
    if num >= -dt and num <= dt then
        return true;
    end
    return false;
end

--算坐标
function MathMgr:isZeroLowQuality(num)
    local dt = 0.01;
    if num >= -dt and num <= dt then
        return true;
    end
    return false;
end


function MathMgr:getFishAngle(startPoint,endPoint)
    local x,y = self:getVectorXY(startPoint,endPoint);
    return self:vectorToAngleHorizontal(x,y);
end


--圆上的点旋转之后的坐标计算
function MathMgr:getNewPoint(config,node)
    local ration = node:getRotation();
    local positionX,positionY = node:getPosition();
    ration = rad(ration);
    local newX = config.dx * math.cos(ration) + config.dy * math.sin(ration) + positionX;
    local newY = -config.dx * math.sin(ration) + config.dy * math.cos(ration) + positionY;
    return newX,newY;
end

function MathMgr:getDis(pos1,pos2)
    return math.sqrt( math.pow(pos1.x - pos2.x,2) + math.pow(pos1.y - pos2.y,2));
end


mgrs.math = nil;
mgrs.singleClass(mgrs,MathMgr,"math");

--endregion
